package com.caijg.controller.user;

import com.caijg.model.dto.LoginDTO;
import com.caijg.model.dto.PasswordDTO;
import com.caijg.model.dto.RegisterDTO;
import com.caijg.model.entity.User;
import com.caijg.model.vo.resp.Result;
import com.caijg.service.IUserService;
import com.caijg.utils.UserContext;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.servlet.http.HttpServletRequest;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * 用户表 前端控制器
 *
 * @author caijg
 * @since 2025-05-07
 */
@RestController
@RequestMapping("/user")
@Slf4j
@Tag(name = "用户模块")
@RequiredArgsConstructor
public class UserController {

    private final IUserService userService;

    @Operation(summary = "发送邮箱验证码")
    @PostMapping("/register/sendEmailCode")
    public Result sendEmailCode(@RequestBody RegisterDTO registerDTO) {
        log.info("sendEmailCode: {}", registerDTO);
        return userService.sendEmailCode(registerDTO);
    }

    @Operation(summary = "用户注册")
    @PostMapping("/register")
    public Result register(@RequestBody RegisterDTO registerDTO) {
        log.info("用户注册: {}", registerDTO);
        return userService.register(registerDTO);
    }

    @Operation(summary = "用户登录")
    @PostMapping("/login")
    public Result login(@RequestBody LoginDTO loginDTO) {
        log.info("用户登录: {}", loginDTO);
        return userService.login(loginDTO);
    }

    @Operation(summary = "修改个人信息")
    @PostMapping("/profile")
    public Result profile(@RequestBody User user) {
        log.info("用户修改个人信息：{}", user);
        return userService.updateProfile(user);
    }

    @Operation(summary = "修改密码")
    @PostMapping("/changePassword")
    public Result changePassword(@RequestBody PasswordDTO passwordDTO) {
        log.info("用户修改密码：{}", passwordDTO);
        return userService.changePassword(passwordDTO);
    }

    @Operation(summary = "退出登录")
    @PostMapping("/logout")
    public Result logout(HttpServletRequest request) {
        log.info("用户退出登录:{}", UserContext.getUid());
        return userService.logout(request);
    }
}